import {
  LOGIN,
  VAlidate_LOGIN,
  NOLOGIN,
  GET_USERS,
  REGISTER_USER,
  GET_IDENTITY,
  ADD_IDENTITY,
} from "../../actionType";
import { userLogin, checkLogin } from "../../../api/login";
import {
  getUserInfo,
  getUserIdentity,
  addUserIdentity,
} from "../../../api/user";
import { userRegister } from "../../../api/register";

export default {
  async [LOGIN]({ commit }, payload) {
    let ret = await userLogin(payload);
    commit(LOGIN, ret.data);
    // console.log(ret);
    if (ret.data.msg === "登录成功") {
      return true;
    } else {
      return false;
    }
  },
  // async [VAlidate_LOGIN]({ commit }, params) {
  //   let ret = await checkLogin(params);
  //   if (ret.data.code == 0) {
  //     commit(LOGIN, ret.data.data);
  //     return true;
  //   } else {
  //     commit(NOLOGIN);
  //     return false;
  //   }
  // },
  async [GET_USERS]({ commit }, params) {
    let ret = await getUserInfo(params);
    console.log(ret);
    commit(GET_USERS, ret.data.data);
    if (ret.data.msg === "数据获取成功") {
      return true;
    } else {
      return false;
    }
  },
  async [REGISTER_USER]({ commit }, params) {
    let ret = await userRegister(params);
    commit(REGISTER_USER, ret);

    if (ret.data.msg === "用户添加成功") {
      return true;
    } else {
      return false;
    }
  },
  async [GET_IDENTITY]({ commit }, params) {
    let ret = await getUserIdentity(params);
    commit(GET_IDENTITY, ret.data.data);
    if (ret.data.msg === "数据获取成功") {
      return true;
    } else {
      return false;
    }
  },
  async [ADD_IDENTITY]({ commit }, params) {
    let ret = await addUserIdentity(params);
    commit(ADD_IDENTITY, {
      identity_id: ret.data.identity_id,
      ...params,
    });
    if (ret.data.msg === "身份添加成功") {
      return true;
    } else {
      return false;
    }
  },
};
